package com.lagou.controller;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;

@Controller
public class MessageController {

    @Autowired
    private AmqpTemplate rabbitTemplate;


    @RequestMapping("/home")
    public String toHome(Model model){
        return "home";
    }

//    @RequestMapping("/pay")
//    public String toPay(Model model, @RequestParam("orderCode")String orderCode){
//        model.addAttribute("orderCode",orderCode);
//        return "pay";
//    }

    @RequestMapping("/rabbit/{message}")
    @ResponseBody
    public String receive(@PathVariable String message) throws UnsupportedEncodingException {

        final MessageProperties messageProperties = MessagePropertiesBuilder.newInstance().setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN)
                .setContentEncoding("utf-8")
                .setHeader("hello", "world")
                .build();

        final Message msg = MessageBuilder
                .withBody(message.getBytes("utf-8"))
                .andProperties(messageProperties)
                .build();

        rabbitTemplate.send("ex.go", "go", msg);

        return "ok";
    }


}
